#include "key.h"

/*
 * 函    数：按键初始化，配置引脚为输入模式，上拉输入
 * 参    数：无
 * 返 回 值：无
 */
void Key_Init(void)
{
    //已经在Cude初始化了，就不走这里进行初始化了
}

/*
 * 函    数：按键扫描,不需要连按
 * 参    数：无
 * 返 回 值：按键值，0~3，无按键按下时返回0xFF
 */
uint8_t Key_Scan(void)
{
    if(KEY0_Read == 0 )
    {
        while (KEY0_Read == 0);
        return 1; 
    }
    if(KEY1_Read == 0 )
    {
        while (KEY1_Read == 0);
        return 2;
    }
    if(KEY2_Read == 0 )
    {
        while (KEY2_Read == 0);
        return 3;
    }
    if(KEY3_Read == 0 )
    {
        while (KEY3_Read == 0);
        return 4;
    }
    return 0;
}

/*
	@函数说明：读取按键值，返回按下对应按键的序号
	@作者：吴广金
	@时间：2023年11月22日
*/
uint8_t Key_GetNum(void)//按键读取
{
    uint8_t keyData = 0;
	if (KEY0_Read == 0 || KEY1_Read == 0 || KEY2_Read == 0 || KEY3_Read == 0)
    {
        key_delay(20);
        if (KEY0_Read == 0 && KEY1_Read == 1 && KEY2_Read == 1 && KEY3_Read == 1)      //选择项S1
            keyData = KEY0_PRES;
        if (KEY0_Read == 1 && KEY1_Read == 0 && KEY2_Read == 1 && KEY3_Read == 1)      //功能页S2
            keyData = KEY1_PRES;
        if (KEY0_Read == 1 && KEY1_Read == 1 && KEY2_Read == 0 && KEY3_Read == 1)      //加S3
            keyData = KEY2_PRES;
        if (KEY0_Read == 1 && KEY1_Read == 1 && KEY2_Read == 1 && KEY3_Read == 0)      //减S4
            keyData = KEY3_PRES;
    }
    return keyData;
}
